extends CanvasLayer

signal continue_game
signal pause_game
@onready var grid_container = $MarginContainer/GridContainer
@onready var data_box = $MarginContainer2/VScrollBar/MarginContainer/data_box
var player = null

func show_menu():
	show_selctor_boxes()
	show_player_info()
	pause_game.emit()
	show()
	pass

# 显示属性选择
func show_selctor_boxes():
	for box in grid_container.get_children():
		if box.is_visible():
			box.queue_free()
	var selector_class = Selector_Class.new()
	for i in range(4):
		var box = %selcetor_module.duplicate()
		var rand = randi_range(0,selector_class.data.size()-1)
		var data = selector_class.data[selector_class.data.keys()[rand]]
		var img = data.group[data.type].img
		var name = data.group.name
		var desc = data.group[data.type].desc
		var detail = data.detail
		var index = randi_range(0,1)
		var value = data.value.split("-")[index] if data.value.contains("-") else data.value
		box.get_node("panel/HBoxContainer/img").texture = load("res://update_panel/assets/"+img+".png")
		box.get_node("panel/HBoxContainer/VBoxContainer/name").text = name
		box.get_node("panel/HBoxContainer/VBoxContainer/desc").text = desc
		box.get_node("panel/detail").text = "[color=green]+"+value+"[/color]"+detail
		box.get_node("panel/select").pressed.connect(_on_select_btn_pressed.bind({
			"name":selector_class.data.keys()[rand],
			"value":value
		}))
		box.show()
		$MarginContainer/GridContainer.add_child(box)
		$MarginContainer.show()
		$reflesh.show()
	pass

# 显示角色属性
func show_player_info():
	for box in data_box.get_children():
		if box.is_visible():
			box.queue_free()
	var datas = player.get_script().get_base_script().get_script_property_list()
	for data in datas:
		if data.name != "player_class.gd":
			var box = %data_template.duplicate()
			box.get_node("./name").text = data.name
			box.get_node("./value").text = str(player[data.name])
			box.show()
			data_box.add_child(box)
	pass

# Called when the node enters the scene tree for the first time.
func _ready():
	player = get_tree().get_first_node_in_group("player")
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func _on_select_btn_pressed(data:Dictionary):
	print(data)
	$MarginContainer.hide()
	$reflesh.hide()
	show_player_info()
	player[data.name] += int(data.value)
	player.update_info()

func _on_reflesh_pressed():
	if player.gold >= 2:
		player.gold -= 2
		player.update_info()
		show_selctor_boxes()
	pass # Replace with function body.

func _on_continue_pressed():
	continue_game.emit()
	hide()
	pass # Replace with function body.

func _on_player_player_upgrade():
	show_menu()
	pass # Replace with function body.
